<?php
require('../include/init.php');
require('check_login.php');
require('../include/pagenavigator.php');
$act = $_REQUEST['act'];
if($act == 'add') {
	if($_FILES['facility_images']['name']) {
		$rand = rand(0, 99999);
		//上传设备图片
		$fileType = substr($_FILES['facility_images']['name'], strpos($_FILES['facility_images']['name'], '.'));
		$Oname = $rand.'_O_'.$time.$fileType;    //原图片名
		$Sname = $rand.'_S_'.$time.$fileType;	//缩略图片名
		$OimagePath = $facilityImgPath.$Oname;  //原图地址
		$SimagePath =$facilityImgPath.$Sname;  //缩略图地址	
		if(is_uploaded_file($_FILES['facility_images']['tmp_name'])){
			if(!move_uploaded_file($_FILES['facility_images']['tmp_name'], $OimagePath)) {
				die('文件上传失败');
			} else {
				resize($OimagePath,$SimagePath,240,200);
			}
		}
		$GLOBALS['db']->query("insert into kyz_facility(facility_images, create_time) values('$Sname', $time)");
		header("Location: $_SERVER[PHP_SELF]");
	}

} elseif ($act == 'edit') {
	$facility_id = $_GET['facility_id'];
	$facility = $GLOBALS['db']->getRow("select * from kyz_facility where facility_id='$facility_id' limit 1");
	$smarty->assign('show_edit_form', true);
	$smarty->assign('facility', $facility);
} elseif ($act == 'save') {
	if($_FILES['facility_images']['name']) {
		$facility_id = $_POST['facility_id'];
		$o_facility_images = $_POST['o_facility_images'];
		$rand = rand(0, 99999);
		//上传设备图片
		$fileType = substr($_FILES['facility_images']['name'], strpos($_FILES['facility_images']['name'], '.'));
		$Oname = $rand.'_O_'.$time.$fileType;    //原图片名
		$Sname = $rand.'_S_'.$time.$fileType;	//缩略图片名
		$OimagePath = $facilityImgPath.$Oname;  //原图地址
		$SimagePath =$facilityImgPath.$Sname;  //缩略图地址	
		$set = '';
		if(is_uploaded_file($_FILES['facility_images']['tmp_name'])){
			if(!move_uploaded_file($_FILES['facility_images']['tmp_name'], $OimagePath)) {
				die('文件上传失败');
			} else {
				resize($OimagePath,$SimagePath,240,200);
				if(!empty($o_facility_images)) {
					$facility_S_images = $o_facility_images;
					$facility_O_images = str_replace('S', 'O', $o_facility_images);
					@unlink($facilityImgPath.$facility_S_images);
					@unlink($facilityImgPath.$facility_O_images);			
				}
				$set .= "facility_images='". $Sname."' ";			
			}
			$GLOBALS['db']->query("update kyz_facility set {$set}where facility_id='$facility_id'");
			header("Location: $_SERVER[PHP_SELF]");
		}
	}

} elseif ($act == 'del') {
	$facility_id = $_GET['facility_id'];
	$facility_images = $GLOBALS['db']->getOne("select facility_images from kyz_facility where facility_id='$facility_id' limit 1");
	if($facility_images) {
		$facility_S_images = $facility_images;
		$facility_O_images = str_replace('S', 'O', $facility_images);
		@unlink($facilityImgPath.$facility_S_images);
		@unlink($facilityImgPath.$facility_O_images);		
	}
	$GLOBALS['db']->query("delete from kyz_facility where facility_id='$facility_id'");
	echo 1;exit();
} elseif ($act == 'del_img') { 
	$facility_id = $_GET['facility_id'];
	$facility_images = $GLOBALS['db']->getOne("select facility_images from kyz_facility where facility_id='$facility_id' limit 1");
	if($facility_images) { 
		$GLOBALS['db']->query("update kyz_facility set facility_images='' where facility_id='$facility_id'");
		$facility_S_images = $facility_images;
		$facility_O_images = str_replace('S', 'O', $facility_images);
		@unlink($facilityImgPath.$facility_S_images);
		@unlink($facilityImgPath.$facility_O_images);
	}
	echo 1;exit();
	
}else {
	if($act == 'show_add_form') {
		$smarty->assign('show_add_form', true);	
	}
	$pagesize = 20;
	$page = $_GET['page'] ? $_GET['page'] : 1;
	$start = ($page-1) * $pagesize;	
	$facility_list = $GLOBALS['db']->getAll("select facility_id, facility_images from kyz_facility order by create_time desc limit $start, $pagesize");
	//print_r($facility_list);exit();
	$total = $GLOBALS['db']->getOne("select count(*) from kyz_facility");
	$pagenavigator = new PageNavigator(array('total' => $total, 'currentPage' => $page, 'pageSize' => $pagesize, 'pageTemplate' => "?page={page}",'url'=>''));	
	$smarty->assign('pagenavigator', $pagenavigator);	
	$smarty->assign('facility_list', $facility_list);

}

$smarty->display('admin/facility_manage.html');
?>